package com.example.administrator.daydayappmarket.game.fragment.fragments.classify.fragment.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.adapter.GameChoiceListViewAdapter;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.bean.GameChoiceMainInfo;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.bean.GameChoiceMainInfoParent;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class GameClassifySecondActivity extends AppCompatActivity implements PullToRefreshListView.OnRefreshListener2{

    @InjectView(R.id.activity_software_category_second_body_head_iamgeveiw0)
    ImageView activitySoftwareCategorySecondBodyHeadIamgeveiw0;
    @InjectView(R.id.activity_software_category_second_body_head_textview0)
    TextView activitySoftwareCategorySecondBodyHeadTextview0;
    @InjectView(R.id.activity_software_category_second_body_head_imagview2)
    ImageView activitySoftwareCategorySecondBodyHeadImagview2;
    @InjectView(R.id.activity_software_category_second_body_layout)
    RelativeLayout activitySoftwareCategorySecondBodyLayout;

    @InjectView(R.id.activity_software_category_second)
    RelativeLayout activitySoftwareCategorySecond;
    @InjectView(R.id.activity_software_category_second_listView)
    PullToRefreshListView activitySoftwareCategorySecondListView;

    private List<GameChoiceMainInfoParent> data;
    private GameChoiceListViewAdapter adapter;
    private RequestQueue requestQueue;
    private int page = 1;

    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_software_category_second);
        ButterKnife.inject(this);
        Intent intent = getIntent();
         id = intent.getStringExtra("id");
        data = new ArrayList<>();
        adapter = new GameChoiceListViewAdapter(data, this);
        activitySoftwareCategorySecondListView.setAdapter(adapter);

        activitySoftwareCategorySecondListView.setMode(PullToRefreshBase.Mode.BOTH);
        activitySoftwareCategorySecondListView.setOnRefreshListener(this);

        initVolley(id);


    }

    private void initVolley(String id) {

        page=1;
        requestQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Contacts.URL_GAME_CLASSIFY_SECOND + id + Contacts.URL_GAME_CLASSIFY_SECOND_END + page, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                if (response != null) {
                    try {
                        JSONArray jsonArray = new JSONArray(response);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                            GameChoiceMainInfo gameChoiceMainInfo = new GameChoiceMainInfo();
                            gameChoiceMainInfo.parseJson(jsonObject1);
                            data.add(gameChoiceMainInfo);
                        }
                        adapter.notifyDataSetChanged();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        stringRequest.setTag("request");
        requestQueue.add(stringRequest);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        requestQueue.cancelAll("request");
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

        data.clear();

        initVolley(id);

        refreshView.onRefreshComplete();

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

        page++;

        requestQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, Contacts.URL_GAME_CLASSIFY_SECOND + id + Contacts.URL_GAME_CLASSIFY_SECOND_END + page, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                if (response != null) {
                    try {
                        JSONArray jsonArray = new JSONArray(response);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                            GameChoiceMainInfo gameChoiceMainInfo = new GameChoiceMainInfo();
                            gameChoiceMainInfo.parseJson(jsonObject1);
                            data.add(gameChoiceMainInfo);
                        }
                        adapter.notifyDataSetChanged();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        stringRequest.setTag("request");
        requestQueue.add(stringRequest);

        refreshView.onRefreshComplete();



    }
}
